package day3.homework;

import java.util.concurrent.locks.ReentrantLock;

public class Gift implements Runnable {
    private int gifts = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (gifts < 10) {
                    return;
                }
                Thread.sleep(100);
                if (Thread.currentThread().getName().equals("小明")) {
                    System.out.println("小明送出第" + (100 - gifts + 1) + "份礼物，还剩" + --gifts + "份礼物");
                } else if (Thread.currentThread().getName().equals("校长")) ;
                System.out.println("校长送出第" + (100 - gifts + 1) + "份礼物，还剩" + --gifts + "份礼物");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
